<!DOCTYPE html>
<html>

<head>
  <title>Quarkus - Command Mode Applications</title>
  <script id="adobe_dtm" src="https://www.redhat.com/dtm.js" type="text/javascript"></script>
  <script src="/assets/javascript/highlight.pack.js" type="text/javascript"></script>
  <META HTTP-EQUIV='Content-Security-Policy' CONTENT="default-src 'none'; script-src 'self' 'unsafe-eval' 'sha256-ANpuoVzuSex6VhqpYgsG25OHWVA1I+F6aGU04LoI+5s=' 'sha256-ipy9P/3rZZW06mTLAR0EnXvxSNcnfSDPLDuh3kzbB1w=' js.bizographics.com https://www.redhat.com assets.adobedtm.com jsonip.com https://ajax.googleapis.com https://www.googletagmanager.com https://www.google-analytics.com https://use.fontawesome.com; style-src 'self' https://fonts.googleapis.com https://use.fontawesome.com; img-src 'self' *; media-src 'self' ; frame-src https://www.googletagmanager.com https://www.youtube.com; frame-ancestors 'none'; base-uri 'none'; object-src 'none'; form-action 'none'; font-src 'self' https://use.fontawesome.com https://fonts.gstatic.com;">
  <META HTTP-EQUIV='X-Frame-Options' CONTENT="DENY">
  <META HTTP-EQUIV='X-XSS-Protection' CONTENT="1; mode=block">
  <META HTTP-EQUIV='X-Content-Type-Options' CONTENT="nosniff">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Quarkus: Supersonic Subatomic Java">
  <meta name="twitter:card" content="summary_large_image">
  <meta name="twitter:site" content="@QuarkusIO"> 
  <meta name="twitter:creator" content="@QuarkusIO">
  <meta property="og:url" content="https://quarkus.io/guides/command-mode-reference" />
  <meta property="og:title" content="Quarkus - Command Mode Applications" />
  <meta property="og:description" content="Quarkus: Supersonic Subatomic Java" />
  <meta property="og:image" content="/assets/images/quarkus_card.png" />
  <link rel="canonical" href="https://quarkus.io/guides/command-mode-reference">
  <link rel="shortcut icon" type="image/png" href="/favicon.ico" >
  <link rel="stylesheet" href="https://quarkus.io/guides/stylesheet/config.css" />
  <link rel="stylesheet" href="/assets/css/main.css" />
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
  <link rel="alternate" type="application/rss+xml"  href="https://quarkus.io/feed.xml" title="Quarkus">
  <script src="https://quarkus.io/assets/javascript/goan.js" type="text/javascript"></script>
  <script src="https://quarkus.io/assets/javascript/hl.js" type="text/javascript"></script>
</head>

<body class="guides">
  <!-- Google Tag Manager (noscript) -->
  <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-NJWS5L"
  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
  <!-- End Google Tag Manager (noscript) -->

  <div class="nav-wrapper">
  <div class="grid-wrapper">
    <div class="width-12-12">
      <input type="checkbox" id="checkbox" />
      <nav id="main-nav" class="main-nav">
  <div class="container">
    <div class="logo-wrapper">
      
        <a href="/"><img src="/assets/images/quarkus_logo_horizontal_rgb_600px_reverse.png" class="project-logo" title="Quarkus"></a>
      
    </div>
    <label class="nav-toggle" for="checkbox">
      <i class="fa fa-bars"></i>
    </label>
    <div id="menu" class="menu">
      <span>
        <a href="/get-started/" class="">Get Started</a>
      </span>
      <span>
        <a href="/guides/" class="active">Guides</a>
      </span>
      <span>
        <a href="/community/" class="">Community</a>
      </span>
      <span>
        <a href="/support/" class="">Support</a>
      </span>
      <span>
        <a href="/blog/" class="">Blog</a>
      </span>
      <span>
        <a href="https://code.quarkus.io" class="button-cta secondary white">Start Coding</a>
      </span>
    </div>
  </div>
      </nav>
    </div>
  </div>
</div>

  <div class="content">
    <div class="guide">
  <div class="width-12-12">
    <h1 class="text-caps">Quarkus - Command Mode Applications</h1>
    <div class="hide-mobile toc"><ul class="sectlevel1">
<li><a href="#writing-command-mode-applications">Writing Command Mode Applications</a>
<ul class="sectlevel2">
<li><a href="#contexts">Contexts</a></li>
<li><a href="#main-method">Main method</a></li>
<li><a href="#the-command-mode-lifecycle">The command mode lifecycle</a></li>
<li><a href="#dev-mode">Dev mode</a></li>
</ul>
</li>
</ul></div>
    <div>
      <div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>This reference covers how to write applications that run and then exit.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="writing-command-mode-applications"><a class="anchor" href="#writing-command-mode-applications"></a>Writing Command Mode Applications</h2>
<div class="sectionbody">
<div class="paragraph">
<p>There are two different approaches that can be used to implement applications
that exit.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Implement <code>QuarkusApplication</code> and have Quarkus run this method automatically</p>
</li>
<li>
<p>Implement <code>QuarkusApplication</code> and a Java main method, and use the Java main method to launch Quarkus</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>In this document the <code>QuarkusApplication</code> instance is referred to as the application main,
and a class with a Java main method is the Java main.</p>
</div>
<div class="paragraph">
<p>The simplest possible command mode application with access to Quarkus API&#8217;s might appear as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="java" class="language-java hljs">import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;

@QuarkusMain    <i class="conum" data-value="1"></i><b>(1)</b>
public class HelloWorldMain implements QuarkusApplication {
  @Override
  public int run(String... args) throws Exception {   <i class="conum" data-value="2"></i><b>(2)</b>
    System.out.println("Hello World");
    return 10;
 }
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>The <code>@QuarkusMain</code> annotation tells Quarkus that this is the main entry point.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>The <code>run</code> method is invoked once Quarkus starts, and the application stops when it finishes.</td>
</tr>
</table>
</div>
<div class="sect2">
<h3 id="contexts"><a class="anchor" href="#contexts"></a>Contexts</h3>
<div class="paragraph">
<p>To get access to your application beans and services, be aware that
a <code>@QuarkusMain</code> instance is an application scoped bean by default. It has access to singletons, application and dependent scoped beans. If you want to interact with beans that requires a request scope put a <code>@ActivateRequestContext</code> on your <code>run()</code> method.</p>
</div>
<div class="paragraph">
<p>This let <code>run()</code> have access to methods like <code>listAll()</code> and <code>query*</code> methods on a Panache Entity. Without it you will eventually get a <code>ContextNotActiveException</code> when accessing such classes/beans.</p>
</div>
</div>
<div class="sect2">
<h3 id="main-method"><a class="anchor" href="#main-method"></a>Main method</h3>
<div class="paragraph">
<p>If we want to use a Java main to run the application main it would look like:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="java" class="language-java hljs">import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.annotations.QuarkusMain;

@QuarkusMain
public class JavaMain {

    public static void main(String... args) {
        Quarkus.run(HelloWorldMain.class, args);
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This is effectively the same as running the <code>HelloWorldMain</code> application main directly, but has the advantage it can
be run from the IDE.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
If a class that implements <code>QuarkusApplication</code> and has a Java main then the Java main will be run.
</td>
</tr>
</table>
</div>
<div class="admonitionblock warning">
<table>
<tr>
<td class="icon">
<i class="fa icon-warning" title="Warning"></i>
</td>
<td class="content">
It is recommended that a Java main perform very little logic, and just
launch the application main. In development mode the Java main will run in a
different ClassLoader to the main application, so may not behave as you would
expect.
</td>
</tr>
</table>
</div>
<div class="sect3">
<h4 id="multiple-main-methods"><a class="anchor" href="#multiple-main-methods"></a>Multiple Main Methods</h4>
<div class="paragraph">
<p>It is possible to have multiple main methods in an application, and select between them at build time.
The <code>@QuarkusMain</code> annotation takes an optional 'name' parameter, and this can be used to select the
main to run using the <code>quarkus.package.main-class</code> build time configuration option. If you don&#8217;t want
to use annotations this can also be used to specify the fully qualified name of a main class.</p>
</div>
<div class="paragraph">
<p>By default the <code>@QuarkusMain</code> with no name (i.e. the empty string) will be used, and if it is not present
and <code>quarkus.package.main-class</code> is not specified then Quarkus will automatically generate a main class
that just runs the application.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
The <code>name</code> of <code>@QuarkusMain</code> must be unique (including the default of the empty string). If you
have multiple <code>@QuarkusMain</code> annotations in your application the build will fail if the names are not
unique.
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect2">
<h3 id="the-command-mode-lifecycle"><a class="anchor" href="#the-command-mode-lifecycle"></a>The command mode lifecycle</h3>
<div class="paragraph">
<p>When running a command mode application the basic lifecycle is as follows:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Start Quarkus</p>
</li>
<li>
<p>Run the <code>QuarkusApplication</code> main method</p>
</li>
<li>
<p>Shut down Quarkus and exit the JVM after the main method returns</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Shutdown is always initiated by the application main thread returning. If you want to run some logic on startup,
and then run like a normal application (i.e. not exit) then you should call <code>Quarkus.waitForExit</code> from the main
thread (A non-command mode application is essentially just running an application that just calls <code>waitForExit</code>).</p>
</div>
<div class="paragraph">
<p>If you want to shut down a running application and you are not in the main thread then you should call <code>Quarkus.asyncExit</code>
in order to unblock the main thread and initiate the shutdown process.</p>
</div>
</div>
<div class="sect2">
<h3 id="dev-mode"><a class="anchor" href="#dev-mode"></a>Dev mode</h3>
<div class="paragraph">
<p>Also for command mode applications the dev mode is supported. When running <code>mvn compile quarkus:dev</code>, the command mode application is executed and on press of the Enter key, is restarted.</p>
</div>
<div class="paragraph">
<p>As command mode applications will often require arguments to be passed on the commandline, this is also possible in dev mode via:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>mvn compile quarkus:dev -Dquarkus.args='--help'</pre>
</div>
</div>
<div class="paragraph">
<p>The same can be achived with Gradle:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>./gradlew quarkusDev --quarkus-args='--help'</pre>
</div>
</div>
</div>
</div>
</div>
    </div>
  </div>
</div>

  </div>

  <div class="content project-footer">
  <div class="footer-section">
    <div class="logo-wrapper">
      <a href="/"><img src="/assets/images/quarkus_logo_horizontal_rgb_reverse.svg" class="project-logo" title="Quarkus"></a>
    </div>
  </div>
  <div class="grid-wrapper">
    <p class="grid__item width-3-12">Quarkus is open. All dependencies of this project are available under the <a href='https://www.apache.org/licenses/LICENSE-2.0' target='_blank'>Apache Software License 2.0</a> or compatible license.<br /><br />This website was built with <a href='https://jekyllrb.com/' target='_blank'>Jekyll</a>, is hosted on <a href='https://pages.github.com/' target='_blank'>Github Pages</a> and is completely open source. If you want to make it better, <a href='https://github.com/quarkusio/quarkusio.github.io' target='_blank'>fork the website</a> and show us what you’ve got.</p>

    
      <div class="width-1-12 project-links">
        <span>Navigation</span>
        <ul class="footer-links width-1-12">
          
            <li><a href="/">Home</a></li>
          
            <li><a href="/guides">Guides</a></li>
          
            <li><a href="/community/#contributing">Contribute</a></li>
          
            <li><a href="/faq">FAQ</a></li>
          
            <li><a href="/get-started">Get Started</a></li>
          
        </ul>
      </div>
    
      <div class="width-1-12 project-links">
        <span>Contribute</span>
        <ul class="footer-links width-1-12">
          
            <li><a href="https://twitter.com/quarkusio">Follow us</a></li>
          
            <li><a href="https://github.com/quarkusio">GitHub</a></li>
          
            <li><a href="/security">Security&nbsp;policy</a></li>
          
        </ul>
      </div>
    
      <div class="width-1-12 project-links">
        <span>Get Help</span>
        <ul class="footer-links width-1-12">
          
            <li><a href="https://groups.google.com/forum/#!forum/quarkus-dev">Forums</a></li>
          
            <li><a href="https://quarkusio.zulipchat.com">Chatroom</a></li>
          
        </ul>
      </div>
    

    
      <div class="width-3-12 more-links">
        <span>Quarkus is made of community projects</span>
        <ul class="footer-links">
          
            <li><a href="https://vertx.io/" target="_blank">Eclipse Vert.x</a></li>
          
            <li><a href="https://microprofile.io" target="_blank">Eclipse MicroProfile</a></li>
          
            <li><a href="https://hibernate.org" target="_blank">Hibernate</a></li>
          
            <li><a href="https://netty.io" target="_blank">Netty</a></li>
          
            <li><a href="https://resteasy.github.io" target="_blank">RESTEasy</a></li>
          
            <li><a href="https://camel.apache.org" target="_blank">Apache Camel</a></li>
          
            <li><a href="https://code.quarkus.io/" target="_blank">And many more...</a></li>
          
        </ul>
      </div>
    
  </div>
</div>
  <div class="content redhat-footer">
  <div class="grid-wrapper">
    <span class="licence">
      <i class="fab fa-creative-commons"></i><i class="fab fa-creative-commons-by"></i> <a href="https://creativecommons.org/licenses/by/3.0/" target="_blank">CC by 3.0</a> | <a href="https://www.redhat.com/en/about/privacy-policy">Privacy Policy</a>
    </span>
    <span class="redhat">
      Sponsored by
    </span>
    <span class="redhat-logo">
      <a href="https://www.redhat.com/" target="_blank"><img src="/assets/images/redhat_reversed.svg"></a>
    </span>
  </div>
</div>


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" integrity="sha384-8gBf6Y4YYq7Jx97PIqmTwLPin4hxIzQw5aDmUg/DDhul9fFpbbLcLh3nTIIDJKhx" crossorigin="anonymous"></script>
  <script type="text/javascript" src="/assets/javascript/mobile-nav.js"></script>
  <script type="text/javascript" src="/assets/javascript/scroll-down.js"></script>
  <script src="/assets/javascript/satellite.js" type="text/javascript"></script>
  <script src="https://quarkus.io/guides/javascript/config.js" type="text/javascript"></script>
  <script src="/assets/javascript/search-filter.js" type="text/javascript"></script>
  <script src="/assets/javascript/back-to-top.js" type="text/javascript"></script>
</body>

</html>
